const CompressionWebpackPlugin = require('compression-webpack-plugin');
const AutoImport = require('unplugin-auto-import/webpack')
const Components = require('unplugin-vue-components/webpack')
const { ElementPlusResolver } = require('unplugin-vue-components/resolvers')

module.exports = {
  publicPath: '/',
  filenameHashing: true,
  productionSourceMap: false,
  outputDir: "code",
  assetsDir: "assets",
  lintOnSave: false,
  configureWebpack: config => {
    config.optimization = {
      splitChunks: {
        chunks: 'all',
        minSize: 20000,
        maxSize: 0,
        minChunks: 1,
        maxAsyncRequests: 5,
        maxInitialRequests: Infinity,
        automaticNameDelimiter: '~',
        name: true,
        cacheGroups: {
          'vue': {
            name: 'vue',
            test: /[\\/]node_modules[\\/]vue[\\/]/,
            priority: -10
          },
          'vue-router': {
            name: 'vue-router',
            test: /[\\/]node_modules[\\/]vue-router[\\/]/,
            priority: -10
          },
          'element-plus': {
            name: 'element-plus',
            test: /[\\/]node_modules[\\/]element-plus[\\/]/,
            priority: -10
          },
          '@element-plu-icons': {
            name: '@element-plus-icon',
            test: /[\\/]node_modules[\\/]@element-plus[\\/]icons-vue[\\/]dist[\\/]lib[\\/]/,
            priority: -10
          },
          'vendors': {
            name: 'vendors',
            test: /[\\/]node_modules[\\/]vendors[\\/]/,
            priority: -20
          },
          'vue-axios': {
            name: 'vue-axios',
            test: /[\\/]node_modules[\\/]vue-axios[\\/]/,
            priority: -20
          },
          'axios': {
            name: 'axios',
            test: /[\\/]node_modules[\\/]axios[\\/]/,
            priority: -20
          }
        }
      }
    },
      config.plugins.push(new CompressionWebpackPlugin({
        algorithm: 'gzip', // 使用gzip压缩
        test: /\.js$|\.html$|\.css$/, // 匹配文件名
        filename: '[path].gz[query]', // 压缩后的文件名(保持原文件名，后缀加.gz)
        minRatio: 1, // 压缩率小于1才会压缩
        threshold: 10240, // 对超过10k的数据压缩
        deleteOriginalAssets: false, // 是否删除未压缩的源文件，谨慎设置，如果希望提供非gzip的资源，可不设置或者设置为false（比如删除打包后的gz后还可以加载到原始资源文件）
      }),
        AutoImport({
          resolvers: [ElementPlusResolver()],
        }),
        Components({
          resolvers: [ElementPlusResolver()],
        }))
  },
  devServer: {
    port: 8888,
    proxy: {
      '/5a1Fazu8AA54nxGko9WTAnF6hhy': {
        target: 'https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy',
        changeOrigin: true,
        pathRewrite: {
          '^/5a1Fazu8AA54nxGko9WTAnF6hhy': ''
        }
      },
      '/auth': {
        target: 'http://open.yeziji.xyz/auth',
        changeOrigin: true,
        pathRewrite: {
          '^/auth': ''
        }
      },
      '/openapi': {
        target: 'http://open.yeziji.xyz/openapi',
        changeOrigin: true,
        pathRewrite: {
          '^/openapi': ''
        }
      },
      '/api': {
        target: 'http://code.a3-507.top/api',
        changeOrigin: true,
        pathRewrite: {
          '^/api': ''
        }
      }
    },
    open: true,
  }
}

// https://zhuanlan.zhihu.com/p/307892763
